<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Quên mật khẩu</title>
    <script type="text/javascript">
        function CheckSignup()
        {
            var s=document.formdangky;


            if(s.capcha.value=='')
            {
                document.getElementById("capcha_error").style.display= "block";
                document.getElementById("capcha_error").innerHTML="Bạn chưa nhập mật mã!";
                s.capcha.focus();
                return false;
            }
            else
                document.getElementById("capcha_error").style.display = "none";
            return true;
        }


    </script>
</head>

<body>
<?php
if(isset($_POST["email"]))
{
    include("process/ketnoi.php");
    $user = $_POST["email"];
if($_POST["email"]=="")
{
    ?>
    <script>
        alert('Email không hợp lệ!');
        location.href="?thamso=quenmatkhau";
    </script>

<?php

}

        $lenh = "select * from user where email='$email'";
        $kq = mysql_query($lenh);
        $row = mysql_fetch_array($kq);
        if($_POST["quen"]&&$_POST['capcha'] == $_SESSION['security_code'])

        //if($row[0]!=0)
        {
            $mk_moi = mt_rand();
            $mk = md5($mk_moi);
            $to = $email;
            $from = 'phuchy220284@gmail.com';
            $subject = 'Lấy lại mật khẩu';
            $message = 'Mật khẩu mới của bạn là:'.$mk_moi;
            $headers  = "From: $from \r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            mail($to,$subject,$message,$header);
            $lenhupdate = "update user set matkhau='$mk' where email='$_POST[email]'";
            mysql_query($lenhupdate);
?>
        <script>
            alert('Đã gởi mật khẩu mới vào mail của bạn!');
        </script>
    <?php

}
else
{
?>
    <script>
        alert('User không đúng hoặc captcha không chính xác');

    </script>
<?php

}

}
?>
<form id="formdangky" onSubmit="return CheckSignup()" name="formdangky" method="post" action="">
    <table width="600px" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFCCCC" style="border-collapse:collapse">
        <tr>
            <td width="550" height="35" align="center" style="font-weight: bold">QUÊN MẬT KHẨU</td>
        </tr>
        <tr>
            <td align="center">Email:
                <label for="email"></label>
                <input name="email" type="text" id="email" size="30" value="<?php   echo $_POST["email"]; ?>" /></td>
        </tr>

        <td align="center">
            <label for="captcha">Captcha</label>
            <input name="capcha" type="text" id="capcha" value="<?php  echo $_POST["capcha"]; ?>" size="32" maxlength="10" /><font color="#FF0000">
      <span class="hide error"
            id="capcha_error"> </span> </font>
        </td>


        </tr>
        <tr>
            <td align="center">

                <img src="user/random_image.php" />
                <br/>
                <?php


                if(isset($_POST['quen']))
                {

                    if($_POST['capcha'] == $_SESSION['security_code'])
                    {
                        echo "Mã lệnh đúng";
                    }
                    else
                    {
                        echo "Mã lênh không chính xác ";
                    }
                }


                ?></td>

        </tr>

        <tr>
            <td align="center"><input type="submit" name="quen" id="quen" value="Quên mật khẩu" /></td>
        </tr>
    </table>
</form>
</body>
</html>